export const guid = () => {
    return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, (c) => {
        const r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
};
export function copyToClipboard(value) {
    let input = document.querySelector(".copy-input") || document.createElement("textarea");
    input.className = "copy-input";
    input.value = value;
    input.style = `position: fixed;top: -100%;`;
    document.body.append(input);
    input.select();
    //虽然要废弃，是目前兼容性最好的方案.
    document.execCommand("copy");
}
/**
 * 自定义toFixed: 解决原生toFixed精度丢失问题
 * @param {*} num
 * @param {*} decimals
 * @returns
 */
export function toFixed(num, decimals) {
    const factor = Math.pow(10, decimals);
    return (Math.round(num * factor) / factor).toFixed(decimals);
}
//格式化千分位符
export function formatPrice(price) {
    return String(toFixed(price, 2)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
//一段比较版本号的代码
export function generateVersionCode() {
    let versionCode = "";
    for (let i = 0; i < 4; i++) {
        versionCode += Math.floor(Math.random() * 10);
    }
    return versionCode;
}
/**
 * 获取小数位
 * @param val
 */
export function getDpLen(val) {
    const str_val = val.toString();
    const split_val = str_val.split(".");
    if (split_val.length > 1) {
        return split_val[1].length;
    }
    else {
        return 0;
    }
}
//节流事件
export function throttle(fn, time = 300) {
    var t = null;
    return function (...rest) {
        if (t)
            return;
        t = setTimeout(() => {
            fn.apply(this, rest);
            t = null;
        }, time);
    };
}
// 获取年度 下拉框
export function getYears(str) {
    const sessionObj = JSON.parse(sessionStorage.getItem("electronParams")) || null;
    let yearVal;
    if (sessionObj) {
        yearVal = sessionObj.params.sjnd; // sessionStorage 中取值
    }
    else {
        const date = new Date();
        yearVal = date.getFullYear();
    }
    const obj = {
        上五年: Number(yearVal) - 5,
        上四年: Number(yearVal) - 4,
        上三年: Number(yearVal) - 3,
        上两年: Number(yearVal) - 2,
        上一年: Number(yearVal) - 1,
        本年: Number(yearVal),
        下一年: Number(yearVal) + 1,
        下两年: Number(yearVal) + 2,
        下三年: Number(yearVal) + 3,
    };
    return obj[str];
}
export default {
    guid,
    formatPrice,
    throttle,
    getYears,
};
